// utils/util.js - 通用工具函数

/**
 * 格式化日期
 */
function formatDate(date, format = 'YYYY-MM-DD') {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day);
}

/**
 * 计算年龄
 */
function calculateAge(birthDate) {
  const birth = new Date(birthDate);
  const today = new Date();
  let age = today.getFullYear() - birth.getFullYear();
  const monthDiff = today.getMonth() - birth.getMonth();
  
  if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
    age--;
  }
  
  return age;
}


/**
 * 获取生肖
 */
function getChineseZodiac(year) {
  const zodiacs = ['猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊'];
  return zodiacs[year % 12];
}

/**
 * 验证手机号
 */
function validatePhone(phone) {
  const reg = /^1[3-9]\d{9}$/;
  return reg.test(phone);
}

/**
 * 验证邮箱
 */
function validateEmail(email) {
  const reg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return reg.test(email);
}

/**
 * 防抖函数
 */
function debounce(func, wait) {
  let timeout;
  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout);
      func(...args);
    };
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
  };
}

/**
 * 节流函数
 */
function throttle(func, limit) {
  let inThrottle;
  return function() {
    const args = arguments;
    const context = this;
    if (!inThrottle) {
      func.apply(context, args);
      inThrottle = true;
      setTimeout(() => inThrottle = false, limit);
    }
  };
}

/**
 * 深拷贝
 */
function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') return obj;
  if (obj instanceof Date) return new Date(obj.getTime());
  if (obj instanceof Array) return obj.map(item => deepClone(item));
  if (typeof obj === 'object') {
    const clonedObj = {};
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        clonedObj[key] = deepClone(obj[key]);
      }
    }
    return clonedObj;
  }
}

/**
 * 获取默认头像
 */
function getDefaultAvatar(gender) {
  return gender === '男' 
    ? '/assets/images/male_young_normal.jpeg' 
    : '/assets/images/female_young_normal.jpeg';
}

/**
 * 格式化收入显示
 */
function formatIncome(income) {
  if (income >= 10000) {
    return `${Math.round(income / 10000)}万`;
  } else if (income >= 1000) {
    return `${Math.round(income / 1000)}千`;
  } else {
    return `${income}`;
  }
}

/**
 * 格式化范围显示
 */
function formatRange(min, max, unit = '') {
  if (min === max) {
    return `${min}${unit}`;
  } else if (min && max) {
    return `${min}-${max}${unit}`;
  } else if (min) {
    return `${min}${unit}以上`;
  } else if (max) {
    return `${max}${unit}以下`;
  } else {
    return '不限';
  }
}

/**
 * 检查是否为今天
 */
function isToday(date) {
  const today = new Date();
  const checkDate = new Date(date);
  return today.toDateString() === checkDate.toDateString();
}


module.exports = {
  formatDate,
  calculateAge,
  getChineseZodiac,
  validatePhone,
  validateEmail,
  debounce,
  throttle,
  deepClone,
  getDefaultAvatar,
  formatIncome,
  formatRange,
  isToday
};